﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RiskMan.Config;
using RiskMan.DataClass.Models;
using RiskMan.DataClass;
using System.Reflection;
using System.Diagnostics;

namespace RiskMan.Providers.QuikIO
{
    public class QuikTerminals : TerminalsEventHandlers
    {
        private Data data;

        public QuikTerminal_0 TransQuikItems_0;
        public QuikTerminal_1 TransQuikItems_1;
        public QuikTerminal_2 TransQuikItems_2;
        public QuikTerminal_3 TransQuikItems_3;
        public QuikTerminal_4 TransQuikItems_4;
        public QuikTerminal_5 TransQuikItems_5;
        public QuikTerminal_6 TransQuikItems_6;
        public QuikTerminal_7 TransQuikItems_7;
        public QuikTerminal_8 TransQuikItems_8;
        public QuikTerminal_9 TransQuikItems_9;

        //private QuikItems quikItems;

        public QuikTerminals ( Data data )
        {
            this.data = data;
        }

        public void OnStatusChanged ( String msgError, String name, bool isConnect, object sender )
        {
            // обработка статуса соединения 
           // Debug.Print( sender + " => " + name + " => " + msgError );
            this.OnTerminalsConnChanged( msgError, name, isConnect, sender );
        }

        public int CountConnected ( )
        {
            int result = 0;

            if ( TransQuikItems_0 != null )
            {
                if ( TransQuikItems_0.IsConnected == true )
                    result++;
            }

            if ( TransQuikItems_1 != null )
            {
                if ( TransQuikItems_1.IsConnected == true )
                    result++;
            }

            if ( TransQuikItems_2 != null )
            {
                if ( TransQuikItems_2.IsConnected == true )
                    result++;
            }
            if ( TransQuikItems_3 != null )
            {
                if ( TransQuikItems_3.IsConnected == true )
                    result++;
            }
            if ( TransQuikItems_4 != null )
            {
                if ( TransQuikItems_4.IsConnected == true )
                    result++;
            }
            if ( TransQuikItems_5 != null )
            {
                if ( TransQuikItems_5.IsConnected == true )
                    result++;
            }
            if ( TransQuikItems_6 != null )
            {
                if ( TransQuikItems_6.IsConnected == true )
                    result++;
            }
            if ( TransQuikItems_7 != null )
            {
                if ( TransQuikItems_7.IsConnected == true )
                    result++;
            }
            if ( TransQuikItems_8 != null )
            {
                if ( TransQuikItems_8.IsConnected == true )
                    result++;
            }
            if ( TransQuikItems_9 != null )
            {
                if ( TransQuikItems_9.IsConnected == true )
                    result++;
            }
            return result;
        }

        public void OnStatusTrans ( object error, int tid, double orderId, string msgError )
        {
            // обработка статуса посланой транзакции
        //    Debug.Print( error + " => " + tid + " => " + orderId + " => " + msgError );
            this.OnTerminalsTrastation( error, tid, orderId, msgError );
        }

        public void OnStatusTransAcc(object error, int tid, double orderId, string msgError, string acc)
        {
            // обработка статуса посланой транзакции
            //    Debug.Print( error + " => " + tid + " => " + orderId + " => " + msgError );
            this.OnTerminalsTranstationAcc (error, tid, orderId, msgError, acc);
        }

        public bool Connects ( System.Windows.Controls.ListBox listBox )
        {
            bool result = true;
            try
            {
                for ( int i = 0; i < data.T2QItems.Count; i++ )
                {
                    #region TransQuikItems_0
                    if ( i == 0 )
                    {
                        TransQuikItems_0 = new QuikTerminal_0( data.T2QItems [ i ].Name, data );
                        TransQuikItems_0.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_0.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_0.OnStatusTransAcc += new TransEventHandlerAcc(OnStatusTransAcc);
                        TransQuikItems_0.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_1
                    if ( i == 1 )
                    {
                        TransQuikItems_1 = new QuikTerminal_1( data.T2QItems [ i ].Name, data );
                        TransQuikItems_1.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_1.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_1.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_2
                    if ( i == 2 )
                    {
                        TransQuikItems_2 = new QuikTerminal_2( data.T2QItems [ i ].Name, data );
                        TransQuikItems_2.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_2.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_2.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_3
                    if ( i == 3 )
                    {
                        TransQuikItems_3 = new QuikTerminal_3( data.T2QItems [ i ].Name, data );
                        TransQuikItems_3.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_3.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_3.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_4
                    if ( i == 4 )
                    {
                        TransQuikItems_4 = new QuikTerminal_4( data.T2QItems [ i ].Name, data );
                        TransQuikItems_4.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_4.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_4.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_5
                    if ( i == 5 )
                    {
                        TransQuikItems_5 = new QuikTerminal_5( data.T2QItems [ i ].Name, data );
                        TransQuikItems_5.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_5.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_5.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_6
                    if ( i == 6 )
                    {
                        TransQuikItems_6 = new QuikTerminal_6( data.T2QItems [ i ].Name, data );
                        TransQuikItems_6.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_6.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_6.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_7
                    if ( i == 7 )
                    {
                        TransQuikItems_7 = new QuikTerminal_7( data.T2QItems [ i ].Name, data );
                        TransQuikItems_7.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_7.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_7.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_8
                    if ( i == 8 )
                    {
                        TransQuikItems_8 = new QuikTerminal_8( data.T2QItems [ i ].Name, data );
                        TransQuikItems_8.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_8.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_8.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                    #region TransQuikItems_9
                    if ( i == 9 )
                    {
                        TransQuikItems_9 = new QuikTerminal_9( data.T2QItems [ i ].Name, data );
                        TransQuikItems_9.OnStatusChanged += new StatusEventHandler( OnStatusChanged );
                        TransQuikItems_9.OnStatusTrans += new TransEventHandler( OnStatusTrans );
                        TransQuikItems_9.Connect( data.T2QItems [ i ].Path );
                    }
                    #endregion
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
            return result;
        }

        public bool Disconnect ( )
        {
            try
            {
                if ( TransQuikItems_0 != null )
                {
                    if ( TransQuikItems_0.IsConnected == true )
                        TransQuikItems_0.Disconnect( );
                    TransQuikItems_0 = null;
                }
                if ( TransQuikItems_1 != null )
                {
                    if ( TransQuikItems_1.IsConnected == true )
                        TransQuikItems_1.Disconnect( );
                    TransQuikItems_1 = null;
                }
                if ( TransQuikItems_2 != null )
                {
                    if ( TransQuikItems_2.IsConnected == true )
                        TransQuikItems_2.Disconnect( );
                    TransQuikItems_2 = null;
                }
                if ( TransQuikItems_3 != null )
                {
                    if ( TransQuikItems_3.IsConnected == true )
                        TransQuikItems_3.Disconnect( );
                    TransQuikItems_3 = null;
                }
                if ( TransQuikItems_4 != null )
                {
                    if ( TransQuikItems_4.IsConnected == true )
                        TransQuikItems_4.Disconnect( );
                    TransQuikItems_4 = null;
                }
                if ( TransQuikItems_5 != null )
                {
                    if ( TransQuikItems_5.IsConnected == true )
                        TransQuikItems_5.Disconnect( );
                    TransQuikItems_5 = null;
                }
                if ( TransQuikItems_6 != null )
                {
                    if ( TransQuikItems_6.IsConnected == true )
                        TransQuikItems_6.Disconnect( );
                    TransQuikItems_6 = null;
                }
                if ( TransQuikItems_7 != null )
                {
                    if ( TransQuikItems_7.IsConnected == true )
                        TransQuikItems_7.Disconnect( );
                    TransQuikItems_7 = null;
                }
                if ( TransQuikItems_8 != null )
                {
                    if ( TransQuikItems_8.IsConnected == true )
                        TransQuikItems_8.Disconnect( );
                    TransQuikItems_8 = null;
                }
                if ( TransQuikItems_9 != null )
                {
                    if ( TransQuikItems_9.IsConnected == true )
                        TransQuikItems_9.Disconnect( );
                    TransQuikItems_9 = null;
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
                return false;
            }

            return true;
        }

        public int SendOrder ( String account, String clientcode, char operation, double price, int lot, string seccode, string classcode )
        {
            try
            {
                int i = 0;
                foreach ( var tmp in data.T2QItems )
                {
                    foreach ( var t in tmp.TradeList )
                        if ( t.Account == account && t.ClientCode == clientcode )
                        {
                            #region send_order_0
                            if ( i == 0 )
                                return TransQuikItems_0.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_1
                            if ( i == 1 )
                                return TransQuikItems_1.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_2
                            if ( i == 2 )
                                return TransQuikItems_2.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_3
                            if ( i == 3 )
                                return TransQuikItems_3.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_4
                            if ( i == 4 )
                                return TransQuikItems_4.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_5
                            if ( i == 5 )
                                return TransQuikItems_5.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_6
                            if ( i == 6 )
                                return TransQuikItems_6.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_7
                            if ( i == 7 )
                                return TransQuikItems_7.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_8
                            if ( i == 8 )
                                return TransQuikItems_8.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_order_9
                            if ( i == 9 )
                                return TransQuikItems_9.SendOrder( operation, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                        }
                    i++;
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
            return 0;
        }

        public void SendMMMarket ( String account, String clientcode, char operation, int lot, string seccode, string classcode )
        {
            try
            {
                int i = 0;
                foreach ( var tmp in data.T2QItems )
                {
                    foreach ( var t in tmp.TradeList )
                        if ( t.Account == account && t.ClientCode == clientcode )
                        {
                            if ( i == 0 )
                                TransQuikItems_0.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 1 )
                                TransQuikItems_1.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 2 )
                                TransQuikItems_2.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 3 )
                                TransQuikItems_3.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 4 )
                                TransQuikItems_4.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 5 )
                                TransQuikItems_5.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 6 )
                                TransQuikItems_6.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 7 )
                                TransQuikItems_7.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 8 )
                                TransQuikItems_8.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                            if ( i == 9 )
                                TransQuikItems_9.MMMarket(operation, classcode, seccode, lot, account, clientcode + "//RiskMan");
                        }
                    i++;
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
        }

        public int SendMMMarketID(String account, String clientcode, char operation, int lot, string seccode, string classcode, string message)
        {
            try
            {
                int i = 0;
                foreach (var tmp in data.T2QItems)
                {
                    foreach (var t in tmp.TradeList)
                        if (t.Account == account && t.ClientCode == clientcode)
                        {
                            if (i == 0)
                                return TransQuikItems_0.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 1)
                                return TransQuikItems_1.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 2)
                                return TransQuikItems_2.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 3)
                                return TransQuikItems_3.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 4)
                                return TransQuikItems_4.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 5)
                                return TransQuikItems_5.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 6)
                                return TransQuikItems_6.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 7)
                                return TransQuikItems_7.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 8)
                                return TransQuikItems_8.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                            if (i == 9)
                                return TransQuikItems_9.MMMarketID(operation, classcode, seccode, lot, account, clientcode + "//" + message);
                        }
                    i++;
                }
            }
            catch (Exception ex)
            {
                cfg.SetLog(String.Format("{0} :    {1}", MethodBase.GetCurrentMethod().Name, ex.ToString()), GetType(), MethodBase.GetCurrentMethod());
            }
            return 0;

        }


        public int SentStopOrder ( String account, String clientcode, char operation, double stopprice, double price, int lot, String seccode, String classcode )
        {
            try
            {
                int i = 0;

                foreach ( var tmp in data.T2QItems )
                {
                    foreach ( var t in tmp.TradeList )
                        if ( t.Account == account && t.ClientCode == clientcode )
                        {
                            #region send_stop_order_0
                            if ( i == 0 )
                                return TransQuikItems_0.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_1
                            if ( i == 1 )
                                return TransQuikItems_1.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_2
                            if ( i == 2 )
                                return TransQuikItems_2.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_3
                            if ( i == 3 )
                                return TransQuikItems_3.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_4
                            if ( i == 4 )
                                return TransQuikItems_4.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_5
                            if ( i == 5 )
                                return TransQuikItems_5.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_6
                            if ( i == 6 )
                                return TransQuikItems_6.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_7
                            if ( i == 7 )
                                return TransQuikItems_7.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_8
                            if ( i == 8 )
                                return TransQuikItems_8.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion
                            #region send_stop_order_9
                            if ( i == 9 )
                                return TransQuikItems_9.SendStopOrder( operation, stopprice, price, lot, seccode, classcode, account, clientcode );
                            #endregion

                        }
                    i++;
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
            return 0;
        }

        public String CancelOrder ( long number, String seccode, String classcode, String account, String clientcode )
        {
            try
            {
                int i = 0;
                foreach ( var tmp in data.T2QItems )
                {
                    foreach ( var t in tmp.TradeList )
                        if ( t.Account == account && t.ClientCode == clientcode )
                        {
                            #region cancel_order_0
                            if ( i == 0 )
                                return TransQuikItems_0.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_1
                            if ( i == 1 )
                                return TransQuikItems_1.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_2
                            if ( i == 2 )
                                return TransQuikItems_2.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_3
                            if ( i == 3 )
                                return TransQuikItems_3.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_4
                            if ( i == 4 )
                                return TransQuikItems_4.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_5
                            if ( i == 5 )
                                return TransQuikItems_5.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_6
                            if ( i == 6 )
                                return TransQuikItems_6.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_7
                            if ( i == 7 )
                                return TransQuikItems_7.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_8
                            if ( i == 8 )
                                return TransQuikItems_8.CancelOrder( number, seccode, classcode );
                            #endregion
                            #region cancel_order_9
                            if ( i == 9 )
                                return TransQuikItems_9.CancelOrder( number, seccode, classcode );
                            #endregion
                        }

                    i++;
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
            return "";
        }

        public String CancelStopOrder ( long number, String seccode, String classcode, String account, String clientcode )
        {
            try
            {

                int i = 0;
                foreach ( var tmp in data.T2QItems )
                {
                    foreach ( var t in tmp.TradeList )
                        if ( t.Account == account && t.ClientCode == clientcode )
                        {
                            #region cancel_stop_order_0
                            if ( i == 0 )
                                return TransQuikItems_0.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_1
                            if ( i == 1 )
                                return TransQuikItems_1.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_2
                            if ( i == 2 )
                                return TransQuikItems_2.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_3
                            if ( i == 3 )
                                return TransQuikItems_3.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_4
                            if ( i == 4 )
                                return TransQuikItems_4.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_5
                            if ( i == 5 )
                                return TransQuikItems_5.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_6
                            if ( i == 6 )
                                return TransQuikItems_6.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_7
                            if ( i == 7 )
                                return TransQuikItems_7.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_8
                            if ( i == 8 )
                                return TransQuikItems_8.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                            #region cancel_stop_order_9
                            if ( i == 9 )
                                return TransQuikItems_9.CancelStopOrder( number, seccode, classcode, account, clientcode );
                            #endregion
                        }
                    i++;
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
            return "";
        }

        public int SendLinkedStop ( char op, string account, string classcode, string seccode, string clientcode, int lot, double stopprice, double price, double takeprice )
        {
            try
            {
                for ( int i = 0; i < data.T2QItems.Count; i++ )
                {
                    for ( int j = 0; j < data.T2QItems [ i ].TradeList.Count; j++ )
                    {
                        if ( data.T2QItems [ i ].TradeList [ j ].Account == account &&
                            data.T2QItems [ i ].TradeList [ j ].ClientCode == clientcode )
                        {
                            if ( i == 0 )
                                return TransQuikItems_0.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 1 )
                                return TransQuikItems_1.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 2 )
                                return TransQuikItems_2.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 3 )
                                return TransQuikItems_3.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 4 )
                                return TransQuikItems_4.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 5 )
                                return TransQuikItems_5.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 6 )
                                return TransQuikItems_6.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 7 )
                                return TransQuikItems_7.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 8 )
                                return TransQuikItems_8.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                            if ( i == 9 )
                                return TransQuikItems_9.SendLinkedOrder( account, classcode, seccode, op, lot, clientcode, stopprice, price, takeprice );
                        }
                    }
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
            return 0;
        }

        public int SendMoveOrder ( String account, String clientcode, long number, double price, int lot, string seccode, string classcode )
        {
            try
            {
                int i = 0;
                foreach ( var tmp in data.T2QItems )
                {
                    foreach ( var t in tmp.TradeList )
                        if ( t.Account == account && t.ClientCode == clientcode )
                        {
                            if ( i == 0 ) return TransQuikItems_0.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 1 ) return TransQuikItems_1.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 2 ) return TransQuikItems_2.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 3 ) return TransQuikItems_3.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 4 ) return TransQuikItems_4.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 5 ) return TransQuikItems_5.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 6 ) return TransQuikItems_6.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 7 ) return TransQuikItems_7.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 8 ) return TransQuikItems_8.SendMoveOrder( classcode, seccode, number, price, lot );
                            if ( i == 9 ) return TransQuikItems_9.SendMoveOrder( classcode, seccode, number, price, lot );
                        }
                    i++;
                }

            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
            return 0;
        }

        public int SendSOWithOtherEmitent ( string classcode, string seccode, string stoppriceclasscode, string stoppriceseccode, string account, string clientcode, double stopprice, double price, double lot )
        {
            clientcode = clientcode.Substring(0, clientcode.IndexOf('|'));
            try
            {
                int i = 0;
                foreach ( var tmp in data.T2QItems )
                {
                    foreach ( var t in tmp.TradeList )
                        if (t.Account == account && t.ClientCode == clientcode)
                        {
                            if ( i == 0 ) return TransQuikItems_0.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 1 ) return TransQuikItems_1.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 2 ) return TransQuikItems_2.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 3 ) return TransQuikItems_3.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 4 ) return TransQuikItems_4.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 5 ) return TransQuikItems_5.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 6 ) return TransQuikItems_6.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 7 ) return TransQuikItems_7.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 8 ) return TransQuikItems_8.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                            if ( i == 9 ) return TransQuikItems_9.SendSOWithOtherEmitent( classcode, seccode, stoppriceclasscode, stoppriceseccode, account, clientcode, stopprice, price, lot );
                        }
                    i++;
                }
            }
            catch ( Exception ex )
            {
                cfg.SetLog( String.Format( "{0} :    {1}", MethodBase.GetCurrentMethod( ).Name, ex.ToString( ) ), GetType( ), MethodBase.GetCurrentMethod( ) );
            }
            return 0;
        }
    }
}
